package ch.fhnw.qualiscan.application.pages;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.components.LoginFormBox;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.core.Page;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * 
 * @author Benjamin Groehbiel
 *
 */
@SuppressWarnings("serial")
public class Invitation extends Page { 

	SurveyModel surveyModel;
	
	SurveyUserRole invitationSurvey = null;
	String thirdParameter;
	
	@Override
	public void initModelsAndHelpers() {
		surveyModel = new SurveyModel();
		thirdParameter = urlLib.getThirdFragment();
	}
	
	@Override
	public void buildLayout() {

		// delete user session if exists, to avoid account mix-ups
		sessionHelper.deleteUserSession();
		componentLib.userLoggedOut();
		
		// check third parameter whether is valid
		if(!thirdParameter.equals("")) {
			SurveyUserRole survey = surveyModel.getSurveyUserRoleByHash(thirdParameter);
			invitationSurvey = survey;
		} else {
			invitationSurvey = null;
		}
		
		// if survey exists, check whether the checksum is correct
		if(invitationSurvey != null) {
			
			addComponent(textHelper.h1(TM.get("invitation.title", invitationSurvey.getSurvey().getTitle())));
			
			if(sessionHelper.getUserSession() == null) {
				addComponent(tab1());
			} else {
				addComponent(tab2());
			}
			
		} else {
			addComponent(tab3());
		}
		
	}
	
	// shown when no session active
	public VerticalLayout tab1() {
		VerticalLayout tab1 = new VerticalLayout();

		HorizontalLayout grid = new HorizontalLayout();
		grid.setWidth("100%");
		grid.setStyleName("InvitationTab1Grid");
		grid.setSpacing(true);
		
		// if user does not exist in db
		if(invitationSurvey.getUser().getFirstname().equals("")) {
			
			// register
			VerticalLayout newAccountBox = new VerticalLayout();
			newAccountBox.setMargin(true);
			newAccountBox.addComponent(textHelper.h2(TM.get("invitation.register.title")));
			newAccountBox.addComponent(textHelper.p(TM.get("invitation.register.desc")));
			
			Button newAccount = new Button(TM.get("invitation.register.button"));
			newAccount.addListener(new ClickListener() {
				public void buttonClick(ClickEvent event) {
					urlLib.setFragment("NewAccount/0/"+ thirdParameter, true);
				}
			});
			newAccount.setIcon(Config.ICON_ADDUSER);
			newAccountBox.addComponent(newAccount);
			
			// login box
			VerticalLayout loginBox = new VerticalLayout();
			loginBox.setMargin(true);
			loginBox.addComponent(textHelper.h2(TM.get("invitation.login")));
			LoginFormBox loginForm = new LoginFormBox(invitationSurvey, true);
			loginBox.addComponent(loginForm);
			grid.addComponent(loginBox);
			
			grid.addComponent(newAccountBox);
		}
		
		// user does exist already
		else {
			VerticalLayout loginBox = new VerticalLayout();
			loginBox.setWidth("500px");
			loginBox.addComponent(textHelper.h2(TM.get("invitation.login")));
			LoginFormBox loginForm = new LoginFormBox();
			loginForm.setUsername(invitationSurvey.getUser().getEmail());
			loginForm.invitationSurvey = invitationSurvey;
			loginBox.addComponent(loginForm);
			grid.addComponent(loginBox);
		}
		
		tab1.addComponent(grid);
		
		return tab1;
	}
	
	// shown when logged in
	public VerticalLayout tab2() {
		VerticalLayout tab2 = new VerticalLayout();
		tab2.addComponent(textHelper.p(TM.get("invitation.loggedin.status", invitationSurvey.getSurvey().getTitle())));
		
		HorizontalLayout Buttons = new HorizontalLayout();
		
		Button yesButton = new Button("goto.dashboard");
		yesButton.setIcon(Config.ICON_HOME);
		yesButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				urlLib.setPage("Dashboard");
			}
		});
		
		Buttons.addComponent(yesButton);
		
		tab2.addComponent(Buttons);
		return tab2;
	}
	
	// show when user does not have the right to access the survey
	public VerticalLayout tab3() {
		VerticalLayout tab3 = new VerticalLayout();
		tab3.setSpacing(true);
		
		tab3.addComponent(textHelper.h1(TM.get("invitation.closed.title")));
		tab3.addComponent(textHelper.p(TM.get("invitation.closed.desc")));
		
		return tab3;
	}

	@Override
	public void setNeededPermissions() {
		
	}

	@Override
	public void initComponents() {
		componentLib.userLoggedOut();
	}

	@Override
	public void thirdParameterChanged(String thirdParameter) {
		
	}

}
